package com.market.coupon.server.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.market.common.core.domain.Result;
import com.market.coupon.common.domain.Req.ComputerReq;
import com.market.coupon.common.domain.TCouponType;
import com.market.coupon.common.domain.enu.CounponEnum;
import com.market.coupon.server.mapper.TCouponTypeMapper;
import com.market.coupon.server.strategy.CouponStrategy;
import com.market.coupon.server.strategy.CouponStrategyContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.market.coupon.server.mapper.TCouponMapper;
import com.market.coupon.common.domain.TCoupon;
import com.market.coupon.server.service.ITCouponService;

/**
 * 优惠券Service业务层处理
 *
 * @author market
 * @date 2024-03-04
 */
@Service
public class TCouponServiceImpl implements ITCouponService
{
    @Autowired
    private TCouponMapper tCouponMapper;

    /**
     * 查询优惠券
     *
     * @param couponId 优惠券主键
     * @return 优惠券
     */
    @Override
    public TCoupon selectTCouponByCouponId(Long couponId)
    {
        return tCouponMapper.selectTCouponByCouponId(couponId);
    }

    /**
     * 查询优惠券列表
     *
     * @param tCoupon 优惠券
     * @return 优惠券
     */
    @Override
    public List<TCoupon> selectTCouponList(TCoupon tCoupon)
    {
        return tCouponMapper.selectTCouponList(tCoupon);
    }

    /**
     * 新增优惠券
     *
     * @param tCoupon 优惠券
     * @return 结果
     */
    @Override
    public int insertTCoupon(TCoupon tCoupon)
    {
        return tCouponMapper.insertTCoupon(tCoupon);
    }

    /**
     * 修改优惠券
     *
     * @param tCoupon 优惠券
     * @return 结果
     */
    @Override
    public int updateTCoupon(TCoupon tCoupon)
    {
        return tCouponMapper.updateTCoupon(tCoupon);
    }

    /**
     * 批量删除优惠券
     *
     * @param couponIds 需要删除的优惠券主键
     * @return 结果
     */
    @Override
    public int deleteTCouponByCouponIds(Long[] couponIds)
    {
        return tCouponMapper.deleteTCouponByCouponIds(couponIds);
    }

    /**
     * 删除优惠券信息
     *
     * @param couponId 优惠券主键
     * @return 结果
     */
    @Override
    public int deleteTCouponByCouponId(Long couponId)
    {
        return tCouponMapper.deleteTCouponByCouponId(couponId);
    }

    @Autowired
    private CouponStrategyContext couponStrategyContext;
    @Autowired
    private TCouponTypeMapper tCouponTypeMapper;

    @Override
    public Result computePrice(ComputerReq req) {
        TCouponType tCouponType = tCouponTypeMapper.selectTCouponTypeByCouponTypeId (req.getCouponTypeId ().longValue());
        //查找出类型中的枚举值，通过枚举值转换为枚举对象获取引用上下文的接口对象
        CouponStrategy couponStrategy = couponStrategyContext.getType (Enum.valueOf (CounponEnum.class, tCouponType.getEnumValue ()));
        //获取的接口对象执行方法
        BigDecimal orderPrice = couponStrategy.doActive (req);
        if (orderPrice.compareTo (req.getOrderPrice ())==0){
            return Result.error (req.getOrderPrice (),"价格不满足优惠条件");
        }
        return Result.success (orderPrice,"优惠后的价格为"+orderPrice);
    }


}
